package com.tz.colawallet.ui.activity;

import android.content.Context;
import android.view.View;

import com.tz.colawallet.R;
import com.tz.colawallet.basecode.base.BaseActivity;
import com.tz.colawallet.basecode.util.SPUtils;
import com.tz.colawallet.basecode.view.CountDownView;
import com.tz.colawallet.bean.CheckLogin;
import com.tz.colawallet.constant.Key;
import com.tz.colawallet.ui.biz.StartBiz;
import com.tz.colawallet.ui.component.DaggerStartComponent;
import com.tz.colawallet.ui.contract.StartContract;
import com.tz.colawallet.ui.module.StartModule;
import com.tz.colawallet.ui.presenter.StartPresenter;

import javax.inject.Inject;


/**
 * Created by chenjian on 17/12/7.
 */

public class StartActivity extends BaseActivity implements StartContract.View {

    @Inject
    StartPresenter presenter;
    @Inject
    StartBiz biz;

    CountDownView countDownView;

    @Override
    protected void setup() {
        super.setup();
        DaggerStartComponent.builder().startModule(new StartModule(this)).build().inject(this);
        presenter.setBiz(biz);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_start;
    }

    @Override
    protected void init() {
        countDownView = findViewById(R.id.countDownView);

        if ((boolean) SPUtils.get(this, Key.FIRST_LOGIN, true)) {
            countDownView
                    .setCountDownTimerListener(new CountDownView.CountDownTimerListener() {
                        @Override
                        public void onStartCount() {
                        }

                        @Override
                        public void onFinishCount() {
                            switchActivity(LoginActivity.class, null);
                            countDownView.finishTime();
                            finish();
                        }
                    });

            countDownView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    switchActivity(LoginActivity.class, null);
                    countDownView.finishTime();
                    finish();
                }
            });

        } else {
            presenter.checkLogin();
        }
    }

    @Override
    protected void initTitle() {

    }

    @Override
    public Context curContext() {
        return this;
    }

    @Override
    public void checkLogin(CheckLogin checkLogin) {
        SPUtils.put(this, Key.TOKEN, checkLogin.getToken());

        countDownView
                .setCountDownTimerListener(new CountDownView.CountDownTimerListener() {
                    @Override
                    public void onStartCount() {
                    }

                    @Override
                    public void onFinishCount() {
                        switchActivity(MainActivity.class, null);
                        countDownView.finishTime();
                        finish();
                    }
                });

        countDownView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                switchActivity(MainActivity.class, null);
                countDownView.finishTime();
                finish();
            }
        });
    }

    @Override
    public void checkFalse() {
        countDownView
                .setCountDownTimerListener(new CountDownView.CountDownTimerListener() {
                    @Override
                    public void onStartCount() {
                    }

                    @Override
                    public void onFinishCount() {
                        switchActivity(LoginActivity.class, null);
                        countDownView.finishTime();
                        finish();
                    }
                });

        countDownView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                switchActivity(LoginActivity.class, null);
                countDownView.finishTime();
                finish();
            }
        });
    }
}

